﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpShare.Diagnostics;

namespace SharpShare.Afp.Protocol.Handlers {
    internal class AfpSetForkParamsRequestHandler : IAfpRequestHandler {
        public byte CommandCode {
            get { return 31; }
        }

        public AfpResultCode Process(IAfpSession session, DsiHeader dsiHeader, AfpStream requestStream, AfpStream responseStream) {
            requestStream.ReadPadding();

            short forkId = requestStream.ReadInt16();
            AfpFileDirectoryBitmap bitmap = requestStream.ReadEnum<AfpFileDirectoryBitmap>();

            long newLength = 0;
            //bool resourceFork = false;

            switch (bitmap) {
                case AfpFileDirectoryBitmap.kFPExtDataForkLenBit:
                    newLength = requestStream.ReadInt64();
                    break;
                case AfpFileDirectoryBitmap.kFPExtRsrcForkLenBit:
                    newLength = requestStream.ReadInt64();
                    //resourceFork = true;
                    break;
                default:
                    return AfpResultCode.FPBitmapErr;
            }

            IAfpFork fork = session.GetFork(forkId);

            if (fork == null) {
                return AfpResultCode.FPObjectNotFound;
            }

            Log.Add(session, EntryType.Debug, "Session '{0}' set parameters '{1}' on fork '{2}' (id: {3}).", session, bitmap, fork.File, forkId);

            fork.DataProvider.SetLength(newLength);

            return AfpResultCode.FPNoErr;
        }
    }
}
